const app = getApp();
import { receive } from '../../../api/coupon'

Component({
  properties: {
    params: {
      type: Object,
      value: {}
    }
  },
  options: {
    addGlobalClass: true
  },
  data: {
    login: false,
    result: []
  },
  ready() {
    const {
      result
    } = this.properties.params
    
    this.setData({
      result: result
    });
  },
  methods: {
    handleReceive (event) {
      const id = event.currentTarget.dataset.id
      const total = event.currentTarget.dataset.total
      const received = event.currentTarget.dataset.received
      
      if (app.login()) {
        if (total == 0 || total > received) {
          receive(id).then(res => {
            app.toast(res.msg, null, 'success')
          })
            .catch(e => {
              app.toast(e.msg)
            })
        }
      } else {
        this.setData({ login: true })
      }
    },
    redirect(event) {
      if (app.login()) {
        app.redirect(event.currentTarget.dataset.link);
      } else {
        if (event.currentTarget.dataset.login == false) {
          app.redirect(event.currentTarget.dataset.link);
        } else {
          this.setData({ login: true });
        }
      }
    }
  }
})